﻿using Aspose.Words;
using Aspose.Words.Saving;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace DOC2TXT.Library
{
    public class Word2HtmlHandler
    {
        public static string Word2Img(string wordpath)
        {
            //Document doc = new Document("f:\\333.doc");
            Document doc = new Document(wordpath);
            //ImageSaveOptions iso = new ImageSaveOptions(SaveFormat.Jpeg);
            //iso.Resolution = 128;
            //iso.PrettyFormat = true;
            //iso.UseAntiAliasing = true;
            HtmlSaveOptions html = new HtmlSaveOptions(SaveFormat.Html);
            var imgpath = "e:/test/" + DateTime.Now.Ticks.ToString() + ".html";
            var result = doc.Save(imgpath, html);
            return imgpath;
            //try
            //{
            //    for (int i = 0; i < doc.PageCount; i++)
            //    {
            //        iso.PageIndex = i;
            //        var imgpath = "e:/test/" + DateTime.Now.Ticks.ToString() + ".jpg";
            //        //doc.Save("D:/test/test" + i + ".jpg", iso);
            //        var result = doc.Save(imgpath,iso);
            //        return imgpath;
            //    }
            //    return "";
            //}
            //catch
            //{
            //    return "";
            //}
        }
        public static string Word2Html(string wordpath)
        {
            try
            {
                Document doc = new Document(wordpath);

                HtmlSaveOptions html = new HtmlSaveOptions(SaveFormat.Html);
                Stream s = new MemoryStream();
                doc.Save(s, html);
                s.Position = 0;
                StreamReader reader = new StreamReader(s);
                var htmlstr = reader.ReadToEnd();
                return htmlstr;
            }
            catch (Exception e)
            {
                return "";
            }

        }
        public static string Word2Txt(string wordpath)
        {
            try
            {
                Document doc = new Document(wordpath);
                return doc.ToString(SaveFormat.Text);
            }
            catch (Exception e)
            {
                return "";
            }

        }
    }
}
